﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Visuals;
using System.Drawing;
using Silvermoon.OpenGL;
using Silvermoon.Utils;
using Silvermoon.Images;
using Silvermoon.Core;

namespace Silvermoon.Controls.Sys
{
    /// <summary>
    /// Control to be used in <see cref="T:KeyboardPanel"/>.
    /// </summary>
    internal class KeyButton : Control
    {
        private BackgroundVisual background;
        private StringVisual label;
        private bool isSelected;
        private string characters = ".";
        private static Color[] selectedButton = ColorUtil.CreatePlasticColors(Color.FromArgb(20, 172, 255));
        private static Color[] normalButtonColors = ColorUtil.CreatePlasticColors(Color.Black);
        private static Color[] sysButtonColors = ColorUtil.CreatePlasticColors(SysColors.SysKeyboardColor);
        private int shiftState;

        public KeyButton()
            : base()
        {
            background = new BackgroundVisual();
            label = new StringVisual { LineMode = LineMode.SingleLine, LineAlignment = Alignment.Center, Alignment = Alignment.Center, Font = FontSprite.Bold };

            Visuals = new Visual[] { background, label };
            TextColor = Color.White;
        }

        public bool IsSystem { get; set; }

        public int ShiftState
        {
            get { return shiftState; }
            set
            {
                if (shiftState != value)
                {
                    shiftState = value;
                    Invalidate(InvalidationFlag.Appearance);
                }
            }
        }

        public bool IsSelected
        {
            get { return isSelected; }
            set
            {
                if (isSelected != value)
                {
                    isSelected = value;
                    Invalidate(InvalidationFlag.Appearance);
                }
            }
        }

        public string Characters
        {
            get { return characters; }
            set
            {
                if (string.IsNullOrEmpty(value)) value = " ";
                if (characters != value) { characters = value; Invalidate(InvalidationFlag.Text); }
            }
        }

        public string Character { get; set; }

        protected override void OnLayout()
        {
            if (CheckFlag(InvalidationFlag.Size))
            {
                background.Size = Control.Scale(size);
                label.Bounds = Control.ScaledRect(1, 1, Width - 2, Height - 2);
            }
            string text = this.characters;
            int shiftState = ShiftState;
            if (shiftState >= text.Length) shiftState = text.Length - 1;

            if (!IsSystem) text = text[shiftState].ToString();
            label.Text = text;
            if (!isSelected)
            {
                background.Colors = IsSystem ? sysButtonColors : normalButtonColors;
                background.Texture = SysImages.Button;
            }
            else
            {
                background.Colors = selectedButton;
                background.Texture = SysImages.GlowButton;
            }

            base.OnLayout();
        }

        public void KeyPress()
        {
            string label = Character ?? this.label.Text;
            Screen.Current.MessageServer.KeyPress(label[0]);
        }

        internal string GetKey()
        {
            if (IsSystem) return Characters;

            string label = Character ?? this.label.Text;
            return label[0].ToString();
        }
    }
}
